package com.sgb.member.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;

import com.sgb.member.BR;
import com.sgb.member.R;
import com.sgb.member.databinding.ItemMemberEnterpriseBinding;
import com.sgb.member.entity.MemberBean;
import com.sgb.member.entity.MemberPersonBean;
import com.sgb.member.weight.MemberCallback;
import com.sgb.member.weight.MemberCommonDialog;
import com.sgb.member.weight.MemberMoreDialog;
import com.sgb.member.weight.MemberTypeLayoutManager;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.StringUtil;

import java.util.List;

public class MemberCommonAdapter extends BaseRecycleAdapter<MemberBean> {

    private final int type;
    private final MemberCallback memberCallback;

    public MemberCommonAdapter(Context context, List<MemberBean> datas, int type, MemberCallback memberCallback) {
        super(context, R.layout.item_member_enterprise, datas);
        this.type = type;
        this.memberCallback = memberCallback;
    }

    @Override
    public void convert(ItemViewHolder holder, final MemberBean entity) {
        ItemMemberEnterpriseBinding binding = (ItemMemberEnterpriseBinding) holder.getmBinding();
        holder.setBinding(BR.viewModel, entity);
        switch (type) {
            case 0:
            case 1:
                // 供方会员
                // 需方会员
                binding.tvFollow.setVisibility(View.GONE);
                binding.ivMemberMore.setVisibility(View.VISIBLE);
                break;
            case 2:
            case 3:
                // 关注企业会员
                // 企业关注会员
                binding.tvFollow.setVisibility(View.GONE);
                binding.ivMemberMore.setVisibility(View.GONE);
                break;
        }
        String name = StringUtil.isEmpty(entity.getContacts()) ? "" : entity.getContacts();
        String phone = StringUtil.isEmpty(entity.getPhone()) ? "" : entity.getPhone();
        binding.tvPhone.setText(String.format("%s %s", name, phone));
        if (entity.getAllCategoryName() != null && entity.getAllCategoryName().size() > 0) {
            binding.childRecyclerView.setVisibility(View.VISIBLE);
            binding.childRecyclerView.setLayoutManager(new MemberTypeLayoutManager(holder.itemView.getContext()));
            binding.childRecyclerView.setAdapter(new BaseRecycleAdapter<String>(holder.itemView.getContext(), R.layout.item_member_type_lable, entity.getAllCategoryName()) {
                @Override
                public void convert(ItemViewHolder holder, String o) {
                    holder.setBinding(BR.itemBean, o);
                }
            });
        } else {
            binding.childRecyclerView.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        super.onBindViewHolder(holder, position);
        ItemMemberEnterpriseBinding binding = (ItemMemberEnterpriseBinding) holder.getmBinding();
        if (position == datas.size() - 1) {
            binding.v.setVisibility(View.VISIBLE);
        } else {
            binding.v.setVisibility(View.GONE);
        }
        /*binding.ivMemberMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 1供方会员 2需方会员
                if (type == 0) {
                    datas.get(position).setMemberUpdateLeverType("2");
                } else if (type == 1) {
                    datas.get(position).setMemberUpdateLeverType("1");
                }
                MemberMoreDialog dialog = new MemberMoreDialog(memberCallback, position, datas.get(position), type == 0);
                dialog.show(v.getContext(), "MemberMoreDialog");
            }
        });*/
        binding.tvModifyMemberType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 1供方会员 2需方会员
                if (type == 0) {
                    datas.get(position).setMemberUpdateLeverType("2");
                } else if (type == 1) {
                    datas.get(position).setMemberUpdateLeverType("1");
                }
                if (memberCallback != null) {
                    memberCallback.onMemberModify(position, datas.get(position));
                }
            }
        });
        binding.tvModifyMemberLever.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 1供方会员 2需方会员
                if (type == 0) {
                    datas.get(position).setMemberUpdateLeverType("2");
                } else if (type == 1) {
                    datas.get(position).setMemberUpdateLeverType("1");
                }
                if (memberCallback != null) {
                    memberCallback.onMemberTransfer(position, datas.get(position));
                }
            }
        });
        //会员解除
//        binding.tvMemberDissolution.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                // 1供方会员 2需方会员
//                if (type == 0) {
//                    datas.get(position).setMemberUpdateLeverType("2");
//                } else if (type == 1) {
//                    datas.get(position).setMemberUpdateLeverType("1");
//                }
//                if (memberCallback != null) {
//                    memberCallback.onMemberDissolution(position, datas.get(position));
//                }
//            }
//        });
        //更多
        binding.ivMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 1供方会员 2需方会员
                MemberBean bean = datas.get(position);
                if (type == 0) {
                    bean.setMemberUpdateLeverType("2");
                } else if (type == 1) {
                    bean.setMemberUpdateLeverType("1");
                }
                memberCallback.onMore(position, bean, binding.ivMore);
            }
        });
        binding.tvFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(v.getContext(), position, datas.get(position));
            }
        });
    }

    public void showDialog(Context context, int pos, MemberBean bean) {
        MemberCommonDialog dialog = new MemberCommonDialog(new MemberCommonDialog.SimpleListener() {
            @Override
            public void onPositive(Object... objects) {
                if (listener != null) {
                    listener.onFollow(pos, bean);
                }
            }
        }, MemberCommonDialog.STYLE_FOLLOW);
        dialog.show(context, "MemberCommonDialog");
    }

    private IFollowListener listener;

    public void setListener(IFollowListener listener) {
        this.listener = listener;
    }

    public interface IFollowListener {
        void onFollow(int pos, MemberBean bean);
    }
}
